import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

export default defineConfig({
  plugins: [vue()],
  server: {
    port: 5173,
    open: true,
    // 添加代理配置
    proxy: {
      '/api': {
        target: 'http://117.72.45.141:8091', // 后端服务器地址
        changeOrigin: true,
        secure: false,
        // 可选：添加日志来调试代理
        configure: (proxy, options) => {
          proxy.on('error', (err, req, res) => {
            console.log('代理错误:', err);
          });
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('发送请求到后端:', req.method, req.url);
          });
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('从后端收到响应:', proxyRes.statusCode, req.url);
          });
        }
      }
    }
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  // 新增优化配置
  optimizeDeps: {
    entries: [
      path.resolve(__dirname, 'index.html') // 显式声明入口
    ],
    exclude: ['vue-router'] // 排除可能引起冲突的依赖
  }
})